package com.joey.concurrency.example.count;

import com.joey.concurrency.annotation.ThreadSafe;
import lombok.extern.slf4j.Slf4j;


import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;


/**
 * @Author hyy
 **/
@Slf4j
@ThreadSafe
public class AtomicReferenceUpdaterExample {

    @lombok.Getter
    public volatile int count = 100;

    private static AtomicIntegerFieldUpdater<AtomicReferenceUpdaterExample> updater
            = AtomicIntegerFieldUpdater.newUpdater(AtomicReferenceUpdaterExample.class,"count");

    private static AtomicReferenceUpdaterExample example=new AtomicReferenceUpdaterExample();

    public static void main(String[] args) {
        if(updater.compareAndSet(example,100,120)){
            log.info("update success 1, {}",example.getCount());
        }
        if(updater.compareAndSet(example,100,120)){
            log.info("update success 2, {}",example.getCount());
        }else{
            log.info("update failed, {}",example.getCount());
        }
    }

}
